angular.module('myApp')

    .controller('queryCtrl', ['$scope', '$http', '$httpParamSerializerJQLike', '$log', 'OpenService', 'CheckboxService',
        'CacheService', 'myConstant', function ($scope, $http, $httpParamSerializerJQLike, $log, OpenService, CheckboxService,
                                                CacheService, myConstant) {
            //视图层变量viewModel
            var vm = $scope.vm = {};
            vm.constant = myConstant;
            //变量初始化
            //分页数据
            vm.pagination = {
                pageSize: 10,
                pageNum: 1
            };
            //查询条件
            vm.queryBean = {};

            vm.queryDetail = queryDetail;
            vm.resetForm = resetForm;

            /*------------------以上配置通用---------------------*/

            vm.auditSucc = auditSucc;
            vm.auditfail = auditfail;
            vm.queryBatchDetail=queryBatchDetail;
            vm.toFailMsgDetail=toFailMsgDetail;
            vm.resetValue=resetValue;

            //缓存数据初始化(需要缓存的key请自定义)
            vm.cached = {
                MERCHANT_CODE: {},
                SPECIALLY_TRANS_CODE:{},
                SPECIALLY_TRANS_CODE_AUDIT:{},
                SPECIALLY_AUDIT_STATUS:{}
            };

            (function initCache() {
                CacheService.initCaches(vm.cached);
            }());

            vm.getCache = function (key) {
                return CacheService.getCache(key);
            };
            /*------------------以上方法名可选择性通用---------------------*/
            //特约商户数据字典
            vm.cache = {
                CUSTPROPERTY: {},
                MERCHANT_RISK_TYPE: {},
                RISK_EVENT_CHANNEL: {},
                INFORMATION_LEVEL: {},
                UPLOAD_MODE: {},
                PERSONAL_RISK_TYPE: {},
                MERCHANT_PROPERTY: {},
                PO_CREDENTIALS_TYPE: {},
                LEGAL_CREDENTIALS_TYPE: {},
                MERCHANT_STATUS: {},
                MERCHANT_TYPE: {},
                CHARGE_TYPE:{},
                SUPPORT_ACCOUNT_TYPE:{},
                EXPANSION_MODE:{},
                COMPLIANCE_RISK:{},
                OPEN_SERVICE_TYPE:{},
                ALL_AREA_CODE:{},
                CLEARING_NETWORK:{}
            };
//批量获取缓存
            var keys = [];
            for (var key in  vm.cache) {
                keys.push(key);
            }
            $log.debug('批量查询缓存');
            $http.post(config.ctx + '/specially/dataDic/cache/get', keys, config.jsonHeader)
                .then(function (response) {
                    var tempMap = response.data.object;
                    for (var key in vm.cache) {
                        if (!angular.isUndefined(tempMap[key])) {
                            vm.cache[key] = tempMap[key];
                        }
                    }
                }, function (response) {
                    $log.error('获取数据%s失败', keys);
                });

            function resetValue(){
                vm.queryBean.merchant =null;
                vm.queryBean.riskType= null;
                vm.queryBean.level = null;
            }

            function queryDetail() {
                var queryForm = document.getElementById('queryForm');
                vm.url = angular.element(queryForm).prop('action');

                //组织参数：查询条件 + 分页数据
                var postData = angular.copy(vm.queryBean);
                postData.pageSize = vm.pagination.pageSize;
                postData.pageNum = vm.pagination.pageNum;
                vm.pagination.queryBean = postData;
                $http.post(vm.url, vm.pagination, config.jsonHeader)
                    .then(function (response) {
                        var data = response.data;
                        if (data.success) {
                            vm.pagination = data.object;
                            //清空选中记录
                            CheckboxService.clearChecked();
                        } else {
                            alert(data.msg);
                        }
                    });

            };


            function queryBatchDetail(bean) {

                $http.post(config.ctx + "/specially/batchMessage/queryDetail", bean, config.jsonHeader)
                    .then(function (response) {
                        var data = response.data;
                        if (data.success) {
                            var item = data.object;

                            $log.debug("响应信息:{}", item);
                            OpenService({
                                modalTitle: '查看明细',
                                modalBody: 'toDetail?trnxCode=' + bean.trnxCode,
                                p: '2',
                                url: '#',
                                item: item,
                                cached: {
                                    MERCHANT_PROPERTY: vm.cache.MERCHANT_PROPERTY,
                                    MERCHANT_TYPE: vm.cache.MERCHANT_TYPE,
                                    SPECIALLY_TRANS_CODE: vm.cached.SPECIALLY_TRANS_CODE,
                                    CUSTPROPERTY: vm.cache.CUSTPROPERTY,
                                    CHANGE_TYPE: vm.cache.CHANGE_TYPE,
                                    MERCHANT_TYPE: vm.cache.MERCHANT_TYPE,
                                    MERCHANT_RISK_TYPE: vm.cache.MERCHANT_RISK_TYPE,
                                    INFORMATION_LEVEL: vm.cache.INFORMATION_LEVEL,
                                    RISK_EVENT_CHANNEL: vm.cache.RISK_EVENT_CHANNEL,
                                    UPLOAD_MODE: vm.cache.UPLOAD_MODE,
                                    PERSONAL_RISK_TYPE: vm.cache.PERSONAL_RISK_TYPE,
                                    ALL_AREA_CODE:vm.cache.ALL_AREA_CODE,
                                    PO_CREDENTIALS_TYPE:vm.cache.PO_CREDENTIALS_TYPE,
                                    LEGAL_CREDENTIALS_TYPE:vm.cache.LEGAL_CREDENTIALS_TYPE,
                                    MERCHANT_STATUS:vm.cache.MERCHANT_STATUS,
                                    CHARGE_TYPE:vm.cache.CHARGE_TYPE,
                                    SUPPORT_ACCOUNT_TYPE:vm.cache.SUPPORT_ACCOUNT_TYPE,
                                    EXPANSION_MODE:vm.cache.EXPANSION_MODE,
                                    COMPLIANCE_RISK:vm.cache.COMPLIANCE_RISK,
                                    OPEN_SERVICE_TYPE:vm.cache.OPEN_SERVICE_TYPE,
                                    CLEARING_NETWORK:vm.cache.CLEARING_NETWORK,
                                    SPECIALLY_AUDIT_STATUS:vm.cached.SPECIALLY_AUDIT_STATUS
                                }
                            }, function (item) {
                            }, '', 'myModalNoSave.html');
                            $log.debug("响应信息:{}", item);
                        } else {
                            alert(data.msg);
                        }
                    });

            };

            function toFailMsgDetail(bean){
                OpenService({
                    modalTitle: '查看审核拒绝原因',
                    modalBody: 'toFailMsgDetail',
                    p: '2',
                    url: '#',
                    item: bean,
                }, function (item) {
                }, '', 'myModalNoSave.html');



            }

            function auditSucc(status) {
                if (CheckboxService.getCheckedNew(vm.pagination.list).length < 1) {

                    alert("请至少选择一条记录！");
                    return;
                }
                var data = CheckboxService.getCheckedNew(vm.pagination.list);
                var trnxCode;
                var flag=false;
                //判断报文报送相同类型 及状态赋值审核状态
                var temp =angular.copy(data);
                angular.forEach(temp, function(obj){
                    console.info("------------"+trnxCode);
                    if(trnxCode == null||trnxCode == ""||trnxCode == undefined){
                        trnxCode = obj.trnxCode;
                        obj.status = status;
                    }else{
                       if(trnxCode == obj.trnxCode){
                           obj.status = status;
                       }else {
                           flag = true;
                           return;

                       }
                    }

                });
                if(flag){
                    alert("请选择相同的报送类型！");
                    return;
                }
                console.info(temp);
                $http.post(config.ctx + "/specially/batchMessage/audit", temp, config.jsonHeader)
                    .then(function (response) {
                        $log.info("审核响应信息:{}", response);
                        var data = response.data;
                        $log.info("审核响应信息1:{}", data);
                        if (data.success) {
                            if(response.data.code == '03'){
                                alert(data.msg);
                            }else if(response.data.code == '01'){
                                alert("操作成功");
                            }else{
                                alert(data.msg);
                            }
                        } else {
                            alert(data.msg);
                        }
                    }, function (response) {
                        vm.queryDetail();
                    });

            }

            function auditfail(status){
                if (CheckboxService.getCheckedNew(vm.pagination.list).length < 1) {

                    alert("请至少选择一条记录！");
                    return;
                }
                var data = CheckboxService.getCheckedNew(vm.pagination.list);
                var trnxCode;
                var flag=false;
                //判断报文报送相同类型 及状态赋值审核状态
                var temp =angular.copy(data);
                angular.forEach(temp, function(obj){
                    console.info("------------"+trnxCode);
                    if(trnxCode == null||trnxCode == ""||trnxCode == undefined){
                        trnxCode = obj.trnxCode;
                        obj.status = status;
                    }else{
                        if(trnxCode == obj.trnxCode){
                            obj.status = status;
                        }else {
                            flag = true;
                            return;

                        }
                    }

                });
                if(flag){
                    alert("请选择相同的报送类型！");
                    return;
                }

                OpenService({
                    modalTitle: '填写审核拒绝明细信息',
                    modalBody: 'toFailMsg',
                    url: 'audit',
                    item: temp,
                    data:temp[0],
                }, function (item) {
                    $log.debug(item);
                    vm.queryDetail();
                }, 'batchModalInstanceCtrl', 'myModalNoFooter.html');



            }



            function resetForm() {
                vm.queryBean = {};
            }

        }]);
angular.module('myApp').controller('batchModalInstanceCtrl',['$scope', '$uibModalInstance', 'modalItem', '$http',
    '$httpParamSerializerJQLike', '$log', 'myConstant', function ($scope, $uibModalInstance, modalItem, $http,
                                                                  $httpParamSerializerJQLike, $log, myConstant) {

        var vm = $scope.vm = {
            modalTitle: 'please change the title',
            modalBody: '#',
            url: '',
            item: {}
        }

        vm = $scope.vm = modalItem;

        vm.constant = myConstant;


        $scope.save = function () {
            angular.forEach(vm.item, function(obj){
                    obj.content = vm.data.content;
            });
            $http.post(vm.url, vm.item,config.jsonHeader)
                .then(function (response) {
                    var data = response.data;
                    if (response.data.success) {
                        alert("操作成功");
                        $uibModalInstance.close(vm.item);
                    } else {
                        alert(data.msg);
                    }
                }, function (response) {
                    alert("操作失败");
                })
        };

        $scope.cancel = function () {
            $uibModalInstance.dismiss('cancel');
        };
        //字符长度限制260
        $scope.checkText = function () {
            if ($scope.vm.data.content.length > 260) {
                alert("字符不能超过260");
                $scope.vm.data.content = $scope.vm.data.content.substr(0, 260);
            }
        };
    }]);